package com.osdp.common.util;

import lombok.NonNull;
import lombok.experimental.UtilityClass;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 *
 * UtilityClass 标记静态方法类。效果如下:1、final类, 2、自动生成一个私有无参构造函数,
 * 3、方法、内部类、变量 标记成statis,4、static import时必须带*号
 */
 @UtilityClass
public class HttpUtil {

    public void serverError(@NonNull HttpServletResponse response, @NonNull String data) throws IOException {
        writeJson(response, 500, data);
    }

    public void writeJson(@NonNull HttpServletResponse response, int httpCode , @NonNull String data){
        response.setStatus(httpCode);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try{
            PrintWriter writer = response.getWriter();
            writer.print(data);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
